package com.huilian.cloud.module.system.controller.admin.frontendbackendpermission;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.huilian.cloud.framework.common.pojo.PageParam;
import com.huilian.cloud.framework.common.pojo.PageResult;
import com.huilian.cloud.framework.common.pojo.CommonResult;
import com.huilian.cloud.framework.common.util.object.BeanUtils;
import static com.huilian.cloud.framework.common.pojo.CommonResult.success;

import com.huilian.cloud.framework.excel.core.util.ExcelUtils;

import com.huilian.cloud.framework.apilog.core.annotation.ApiAccessLog;
import static com.huilian.cloud.framework.apilog.core.enums.OperateTypeEnum.*;

import com.huilian.cloud.module.system.controller.admin.frontendbackendpermission.vo.*;
import com.huilian.cloud.module.system.dal.dataobject.frontendbackendpermission.FrontendBackendPermissionDO;
import com.huilian.cloud.module.system.service.frontendbackendpermission.FrontendBackendPermissionService;

@Tag(name = "管理后台 - 前后端关联标识")
@RestController
@RequestMapping("/sys/frontend-backend-permission")
@Validated
public class FrontendBackendPermissionController {

    @Resource
    private FrontendBackendPermissionService frontendBackendPermissionService;

    @PostMapping("/create")
    @Operation(summary = "创建前后端关联标识")
    @PreAuthorize("@ss.hasPermission('sys:frontend-backend-permission:create')")
    public CommonResult<String> createFrontendBackendPermission(@Valid @RequestBody FrontendBackendPermissionSaveReqVO createReqVO) {
        return success(frontendBackendPermissionService.createFrontendBackendPermission(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新前后端关联标识")
    @PreAuthorize("@ss.hasPermission('sys:frontend-backend-permission:update')")
    public CommonResult<Boolean> updateFrontendBackendPermission(@Valid @RequestBody FrontendBackendPermissionSaveReqVO updateReqVO) {
        frontendBackendPermissionService.updateFrontendBackendPermission(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除前后端关联标识")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sys:frontend-backend-permission:delete')")
    public CommonResult<Boolean> deleteFrontendBackendPermission(@RequestParam("id") String id) {
        frontendBackendPermissionService.deleteFrontendBackendPermission(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得前后端关联标识")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sys:frontend-backend-permission:query')")
    public CommonResult<FrontendBackendPermissionRespVO> getFrontendBackendPermission(@RequestParam("id") String id) {
        FrontendBackendPermissionDO frontendBackendPermission = frontendBackendPermissionService.getFrontendBackendPermission(id);
        return success(BeanUtils.toBean(frontendBackendPermission, FrontendBackendPermissionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得前后端关联标识分页")
    @PreAuthorize("@ss.hasPermission('sys:frontend-backend-permission:query')")
    public CommonResult<PageResult<FrontendBackendPermissionRespVO>> getFrontendBackendPermissionPage(@Valid FrontendBackendPermissionPageReqVO pageReqVO) {
        PageResult<FrontendBackendPermissionDO> pageResult = frontendBackendPermissionService.getFrontendBackendPermissionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FrontendBackendPermissionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出前后端关联标识 Excel")
    @PreAuthorize("@ss.hasPermission('sys:frontend-backend-permission:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFrontendBackendPermissionExcel(@Valid FrontendBackendPermissionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FrontendBackendPermissionDO> list = frontendBackendPermissionService.getFrontendBackendPermissionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "前后端关联标识.xls", "数据", FrontendBackendPermissionRespVO.class,
                        BeanUtils.toBean(list, FrontendBackendPermissionRespVO.class));
    }

}